package com.example.fish.service.impl;

import com.example.fish.mapper.CollectionMapper;
import com.example.fish.mapper.SecretMapper;
import com.example.fish.pojo.Collection;
import com.example.fish.pojo.CollectionExample;
import com.example.fish.pojo.Secret;
import com.example.fish.service.CollectionService;
import com.example.fish.service.ex.CollectionExistsException;
import com.example.fish.service.ex.CollectionInsertException;
import com.example.fish.service.ex.CollectionNotExitsException;
import com.example.fish.vo.CollectionOwn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private SecretMapper secretMapper;
    @Override
    public void collectSecret(Integer secretid, Integer userid) {
//       先判断有没有
        CollectionExample example = new CollectionExample();
        example.createCriteria().andUseridEqualTo(userid).andSecretidEqualTo(secretid);
        int i = collectionMapper.countByExample(example);
        if (i != 0 ){
            throw new CollectionExistsException("收藏过");
        }
        Collection collection = new Collection();
        collection.setSecretid(secretid);
        collection.setUserid(userid);
//        时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.now();
        String formattedDateTime = dateTime.format(formatter);
        collection.setCollectiontime(formattedDateTime);
//        保存
        int insert = collectionMapper.insert(collection);
        if (insert == 0){
            throw new CollectionInsertException("收藏失败");
        }
    }

    @Override
    public void cancelCollect(Integer secretid, Integer userid) {
        CollectionExample example = new CollectionExample();
        example.createCriteria().andUseridEqualTo(userid).andSecretidEqualTo(secretid);
        int i = collectionMapper.deleteByExample(example);
        if (i == 0){
            throw new CollectionNotExitsException("没有收藏过");
        }
    }

    @Override
    public Boolean isCollected(Integer secretid, Integer userid) {
        CollectionExample example=new CollectionExample();
        example.createCriteria().andUseridEqualTo(userid).andSecretidEqualTo(secretid);
        int i = collectionMapper.countByExample(example);
        if (i == 0){
//            没被收藏
            return false;
        }else {
            return true;
        }
    }

    /***
     * 个人信息页面用于展示个人收藏的
     * @return list
     */

    @Override
    public List<CollectionOwn> findCollectionByUserid(Integer userid) {
//        根据userid查询collection list
        CollectionExample example = new CollectionExample();
        example.createCriteria().andUseridEqualTo(userid);
        List<Collection> collections = collectionMapper.selectByExample(example);
        List<CollectionOwn> list = new ArrayList<>();
//        封装
        for (Collection collection : collections) {
            CollectionOwn collect = new CollectionOwn();
            collect.setSecretid(collection.getSecretid());
            collect.setCollectionid(collection.getCollectionid());
            collect.setCollectiontime(collection.getCollectiontime());
//            根据secretid查询
            Secret secret = secretMapper.selectByPrimaryKey(collection.getSecretid());
            collect.setTitle(secret.getTitle());
            collect.setType(secret.getType());
            list.add(collect);
        }
        return list;
    }

    @Override
    public PageInfo<CollectionOwn> findOwnByPage(Integer pageNum, Integer userid) {
        Integer pageSize = 5;
        CollectionExample example = new CollectionExample();
        example.createCriteria().andUseridEqualTo(userid);
//        总条数
        int i = collectionMapper.countByExample(example);
        PageHelper.startPage(pageNum, pageSize);
        List<Collection> collections = collectionMapper.selectByExample(example);
        List<CollectionOwn> list = new ArrayList<>();
//        封装
        for (Collection collection : collections) {
            CollectionOwn collect = new CollectionOwn();
            collect.setSecretid(collection.getSecretid());
            collect.setCollectionid(collection.getCollectionid());
            collect.setCollectiontime(collection.getCollectiontime());
//            根据secretid查询
            Secret secret = secretMapper.selectByPrimaryKey(collection.getSecretid());
            if (secret == null){
                continue;
            }
            collect.setTitle(secret.getTitle());
            collect.setType(secret.getType());
            list.add(collect);
        }
        PageInfo<CollectionOwn> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }
}
